package cn.xi.cana.listener;

import cn.xi.cana.config.RabbitMqConfig;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;


@CanalEventListener
public class AdListener {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @ListenPoint(schema = "changgou_business", table = {"tb_ad"})
    public void adUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
//        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
//        beforeColumnsList.forEach((c) ->
//                System.out.println("更改数据前:" + c.getName() + "::" + c.getValue())
//        );
//        rowData.getAfterColumnsList().forEach((c) ->
//                System.out.println("更改数据后:" + c.getName() + "::" + c.getValue())
//        );

        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            if (column!=null){
                if ("position".equals(column.getName())){
                    System.out.println("将改变后的值发送到交换机");
                    rabbitTemplate.convertAndSend("", RabbitMqConfig.AD_UPDATE_QUEUE,column.getValue());
                }
            }
        }


    }

}
